// import Axios from 'axios'
import * as types from './types.js'

export default {
  /*获取购物车所有商品*/
  get_all_cart: ({ commit }) => {
    localStorage.getItem('cart')
      ? commit(types.GET_ALL_CART, JSON.parse(localStorage.getItem('cart')))
      : commit(types.GET_ALL_CART, {})
    // localStorage.getItem('cart')
    //   ? commit(types.GET_ALL_CART, JSON.parse(localStorage.getItem('cart')))
    //   : Axios.get('./data/cart-lists.json')
    //       .then(data => {
    //         data.data.result.forEach(item => {
    //           // console.log(item)
    //           commit(types.CREATE_GOODS_TO_CART, item)
    //           commit(types.UPDATE_CART)
    //         })
    //       })
    //       .catch(() => {
    //         commit(types.CREATE_GOODS_TO_CART, '')
    //       })
    localStorage.getItem('cartSelected')
      ? commit(types.GET_ALL_CART_SELECTED, JSON.parse(localStorage.getItem('cartSelected')))
      : commit(types.GET_ALL_CART_SELECTED, {})
    // localStorage.getItem('cartSelected')
    //   ? commit(types.GET_ALL_CART_SELECTED, JSON.parse(localStorage.getItem('cartSelected')))
    //   : Axios.get('./data/cart-selected-lists.json')
    //       .then(data => {
    //         data.data.result.forEach(item => {
    //           // console.log(item)
    //           commit(types.CREATE_GOODS_TO_SELECTED, item)
    //           commit(types.UPDATE_CART)
    //         })
    //       })
    //       .catch(() => {
    //         commit(types.CREATE_GOODS_TO_SELECTED, '')
    //       })
  },

  // 清空购物车
  clear_cart: ({ commit }) => {
    commit(types.GET_ALL_CART, {})
    commit(types.GET_ALL_CART_SELECTED, {})
    commit(types.UPDATE_CART)
  },
  /*添加商品到购物车*/
  create_goods_to_cart: ({ commit }, goods) => {
    commit(types.CREATE_GOODS_TO_CART, goods)
    commit(types.UPDATE_CART)
  },
  /*更新购物车促销信息*/
  update_goods_couponid: ({ commit }, goods) => {
    commit(types.UPDATE_GOODS_COUPONID, goods)
  },

  /*添加商品到购物车选中列表*/
  create_goods_to_selected: ({ commit }, goods) => {
    commit(types.CREATE_GOODS_TO_SELECTED, goods)
    commit(types.UPDATE_CART)
  },
  /*删除购物车的商品*/
  delete_goods_from_cart: ({ commit }, goods) => {
    commit(types.DELETE_GOODS_FROM_CART, goods)
    commit(types.UPDATE_CART)
  },
  /*删除购物车选中的商品*/
  delete_goods_from_selected: ({ commit }, goods) => {
    commit(types.DELETE_GOODS_FROM_SELECTED, goods)
    commit(types.UPDATE_CART)
  },
  /*购物车商品+1*/
  add_goods_from_cart: ({ commit }, goods) => {
    // console.log(goods, plus)
    commit(types.ADD_GOODS_FROM_CART, goods)
    commit(types.UPDATE_CART)
  },
  /*购物车选中商品+1*/
  add_goods_from_selected: ({ commit }, goods) => {
    commit(types.ADD_GOODS_FROM_SELECTED, goods)
    commit(types.UPDATE_CART)
  },
  /*购物车商品-1*/
  reduce_goods_from_cart: ({ commit }, goods) => {
    commit(types.REDUCE_GOODS_FROM_CART, goods)
    commit(types.UPDATE_CART)
  },
  /*购物车选中商品-1*/
  reduce_goods_from_selected: ({ commit }, goods) => {
    commit(types.REDUCE_GOODS_FROM_SELECTED, goods)
    commit(types.UPDATE_CART)
  },
  modify_goods_num_from_cart: ({ commit }, data) => {
    commit(types.MODIFY_GOODS_NUM_FROM_CART, data)
    commit(types.UPDATE_CART)
  },
  /*更新本地购物车*/
  update_cart: ({ commit }) => {
    commit(types.UPDATE_CART)
  }
}
